#include<bits/stdc++.h>
using namespace std;
const int N = 10004;

vector<int> edges[N];
int n,m;
int low[N],nums[N];
bool insta[N];
int sta[N];
int top = 0;
int tot = 1;
int scc[N];
int sccnt[N];
int sc_sum = 0;

vector<int> edges2[N];
int in[N];
int sortNum[N];


int fath[N];
int find(int x){
    if(x==fath[x]) return x;
    fath[x] = find(fath[x]);
    return fath[x];
}
void merge(int x,int y){
    fath[x] = find(fath[y]);
}


void tarjan(int cur){
    nums[cur] = low[cur] = tot++;
    sta[top++] = cur;
    insta[cur] = true;

    for(auto t:edges[cur]){
        if(low[t]==0){
            tarjan(t);
            low[cur] = min(low[cur],low[t]);
        }
        else if(insta[t]){
            low[cur] = min(low[cur],nums[t]);
        }
    }
    if(nums[cur]==low[cur]){
        int y = 0;
        int count = 0;
        ++sc_sum;
        do{
            count++;
            y = sta[--top];
            insta[y] = false;
            scc[y] = sc_sum;
        }while(y!=cur);
        sccnt[scc[cur]] = count;
    }
}


int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int s,t;
        scanf("%d%d",&s,&t);
        edges[s].emplace_back(t);
    }
    memset(low,0,sizeof(low));
    memset(insta,0,sizeof(insta));
    for(int i=1;i<=n;i++){
        if(low[i]==0) tarjan(i);
    }



    // 拓扑排序
    memset(in,0,sizeof(in));
    for(int i=1;i<=n;i++){
        for(auto t:edges[i]){
            if(scc[i]!=scc[t]){
                edges2[scc[i]].emplace_back(scc[t]);
                in[scc[t]]++;
            }
        }
    }
    //检验连通性
    for(int i=1;i<=sc_sum;i++) fath[i] = i;

    for(int i=1;i<=sc_sum;i++){
        for(auto t:edges2[i]){
            merge(i,t);
        }
    }
    int root = find(1);
    for(int i=2;i<=sc_sum;i++){
        if(find(i)!=root){
            printf("0\n");
            return 0;
        }
    }


    queue<int> que;
    for(int i=1;i<=sc_sum;i++){
        if(in[i]==0) que.push(i);
    }
    memset(sortNum,0,sizeof(sortNum));
    int epoch = 1;
    while(!que.empty()){
        int size = que.size();
        for(int i=0;i<size;i++){
            int cur = que.front();
            que.pop();
            for(auto e:edges2[cur]){
                --in[e];
                if(in[e]==0) {
                    sortNum[e] = epoch;
                    que.push(e);
                }
            }
        }
        epoch++;
    }

    int maxepoch = 0;
    int mcnt = 0;
    int ans = 0;
    for(int i=1;i<=sc_sum;i++){
        maxepoch = max(maxepoch,sortNum[i]);
    }
    for(int i=1;i<=sc_sum;i++){
        if(sortNum[i]==maxepoch) {
            mcnt++;
            ans = sccnt[i];
        }
    }
    if(mcnt==1){
        printf("%d\n",ans);
        return 0;
    }
    printf("0\n");
    return 0;
}